package com.teamin.web.controller;


import com.mysql.cj.core.util.StringUtils;
import com.teamin.config.ErrorCode;
import com.teamin.entity.LabelEntity;
import com.teamin.service.CircleService;
import com.teamin.service.UserService;
import com.teamin.service.WebsocketService;
import com.teamin.service.impl.WebsocketServiceImpl;
import com.teamin.service.labelService.LabelService;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.model.ErrorInfo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(description = "跟圈子的标签相关接口",tags = "标签")
public class LabelController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private LabelService labelService;

	@Autowired
	private CircleService circleService;

	@Autowired
	private UserService userService;


	@Autowired
	private WebsocketServiceImpl websocketService ;

	/**
	 * 添加标签到圈子中
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/v1/circles/{circleId}/label", method = RequestMethod.POST)
	public LabelEntity addLabelToCircle(@PathVariable("circleId") Long circleId, @Valid @ModelAttribute LabelEntity labelEntity, @RequestAttribute("loginUserId") Long loginUserId,
										@RequestAttribute("userFlag") String userFlag) throws Exception{

		//标签名不能为空
		if(StringUtils.isNullOrEmpty(labelEntity.getLabelName())) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.LABEL_NAME_IS_NULL));
		}

		// 不属于圈子
		if (!userService.userInCircle(loginUserId, circleId)) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FIND_USER_FROM_CIRCLE_NULL));
		}

		// 圈子中是否存在此标签
		if (labelService.existCircleLabelName(circleId,labelEntity.getLabelName())) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.CIRCLE_EXIST_LABEL));
		}
		labelService.addLabelToCircle(labelEntity,loginUserId) ;

		LabelEntity labelEntity2 = labelService.selectByPrimaryKey(labelEntity.getLabelId());;
		websocketService.sendCircle(loginUserId,circleId, WebsocketService.CirclePropertise.CIRCLE_LABEL, WebsocketService.Control.ADD,labelEntity2,userFlag);

		return labelEntity2 ;
	}


	/**
	 * 删除标签到圈子中
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/v1/circles/{circleId}/label/{labelId}", method = RequestMethod.DELETE)
	public void deleteLabelToCircle(@PathVariable("circleId") Long circleId,@PathVariable("labelId") Long labelId,@RequestAttribute("loginUserId") Long loginUserId,
									@RequestAttribute("userFlag") String userFlag) throws Exception{

		// 不属于圈子
		if (!userService.userInCircle(loginUserId, circleId)) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FIND_USER_FROM_CIRCLE_NULL));
		}

		// 圈子中是否存在此标签
		if (!labelService.existCircleLabel(circleId,labelId)) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.CIRCLE_NOT_EXIST_LABEL));
		}

		LabelEntity labelEntity2 = labelService.selectByPrimaryKey(labelId);;

		websocketService.sendCircle(loginUserId,circleId, WebsocketService.CirclePropertise.CIRCLE_LABEL, WebsocketService.Control.DELETE,labelEntity2,userFlag);
		labelService.deleteByPrimaryKey(circleId,labelId,loginUserId) ;
	}


	/**
	 * 查找圈子中的标签
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/v1/circles/{circleId}/label", method = RequestMethod.GET)
	public List<LabelEntity> selectLabelByCircle(@PathVariable("circleId") Long circleId, Long labelId, @RequestAttribute("loginUserId") Long loginUserId, String labelNameFilter) throws Exception{

		// 不属于圈子
		if (!userService.userInCircle(loginUserId, circleId)) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FIND_USER_FROM_CIRCLE_NULL));
		}

		return labelService.selectByCircle(circleId,labelNameFilter) ;
	}

	/**
	 * 更新标签
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/v1/circles/{circleId}/label/{labelId}", method = RequestMethod.PUT)
	public LabelEntity updateLabel(@PathVariable("circleId") Long circleId,@PathVariable("labelId") Long labelId, @Valid @ModelAttribute LabelEntity labelEntity, @RequestAttribute("loginUserId") Long loginUserId,
								   @RequestAttribute("userFlag") String userFlag) throws Exception{

		//标签名不能为空
		if(StringUtils.isNullOrEmpty(labelEntity.getLabelName())) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.LABEL_NAME_IS_NULL));
		}

		// 不属于圈子
		if (!userService.userInCircle(loginUserId, circleId)) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FIND_USER_FROM_CIRCLE_NULL));
		}

		// 圈子中是否存在此标签
		if (labelService.existCircleLabelName(circleId,labelEntity.getLabelName())) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.CIRCLE_EXIST_LABEL));
		}

		// 圈子中是否存在此标签
		if (!labelService.existCircleLabel(circleId,labelId)) {
			throw new GlobalErrorInfoException(
					new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.CIRCLE_NOT_EXIST_LABEL));
		}
		labelService.updateByPrimaryKeySelective(labelEntity,loginUserId) ;

		//数据同步通知
		LabelEntity labelEntity2 = labelService.selectByPrimaryKey(labelEntity.getLabelId());;
		websocketService.sendCircle(loginUserId,circleId, WebsocketService.CirclePropertise.CIRCLE_LABEL, WebsocketService.Control.MODIFY,labelEntity2,userFlag);
		return labelEntity2 ;
	}

}